package Sort;

public class CountSort {

    public void countSort(int[] array) {
        int min = array[0];
        int max = array[0];
        //1.求最值
        for (int i = 0; i < array.length; i++) {
            if(array[i] < min) {
                min = array[i];
            }
            if(array[i] > max) {
                max = array[i];
            }
        }
        //2.定义计数数组 进行初始化
        int[] count = new int[max-min+1];
        for (int i = 0; i < array.length; i++) {
            int index = array[i]-min;
            count[index]++;
        }
        //3.遍历技术数组
        int k = 0;//表示array数组的下标
        for (int i = 0; i < count.length; i++) {
            while(count[i] != 0) {
                array[k] = i+min;
                k++;
                count[i]--;
            }
        }
    }
}
